package io.renren.modules.canteens.controller;

import io.renren.common.annotation.LogOperation;
import io.renren.common.utils.ExcelUtils;
import io.renren.common.utils.Result;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.canteens.dto.BatchOrderDTO;
import io.renren.modules.canteens.dto.BatchOrdermxDTO;
import io.renren.modules.canteens.dto.FoodDTO;
import io.renren.modules.canteens.excel.BatchOrdermxExcel;
import io.renren.modules.canteens.service.BatchOrderService;
import io.renren.modules.canteens.service.BatchOrdermxService;
import io.renren.modules.canteens.service.FoodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;


/**
 * 批量采购明细表 
 *
* @author 翟文闯 619556390@QQ.com
 * @since 1.0.0 2020-01-03
 */
@RestController
@RequestMapping("canteens/batchordermx")
@Api(tags="批量采购表")
public class BatchOrdermxController {
    @Autowired
    private BatchOrdermxService batchOrdermxService;
    @Autowired
    private BatchOrderService batchOrderService;
    @Autowired
    private FoodService foodService;
    @PostMapping("save")
    @ApiOperation("保存")
    @LogOperation("保存")
    public Result save(@RequestBody BatchOrdermxDTO dto){
        Result result = new Result();

        FoodDTO foodDTO = foodService.getName(dto.getFoodName());
        if(null ==foodDTO){
            result.setMsg(dto.getFoodName()+"菜品不存在");
            result.setCode(5001);
            return result;
        }else {
            dto.setUnit(foodDTO.getUnit());
            if(null==dto.getPurchaseNum()|| null ==dto.getUnitPrice()){
                result.setMsg(dto.getFoodName()+"数量或价格不能为空");
                result.setCode(5001);
                return result;
            }
            dto.setMoney(dto.getPurchaseNum()*dto.getUnitPrice());
            dto.setFoodId(foodDTO.getFoodId());
            batchOrdermxService.save(dto);
            result.setMsg(dto.getFoodName()+"保存成功");
            result.setCode(0);
            return result;
        }
    }

    @PostMapping("update")
    @ApiOperation("修改")
    @LogOperation("修改")
    public Result update(@RequestBody BatchOrdermxDTO dto){
        Result result = new Result();
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        FoodDTO foodDTO = foodService.getName(dto.getFoodName());
        if(null ==foodDTO){
            result.setMsg(dto.getFoodName()+"菜品不存在");
            result.setCode(5001);
            return result;
        }else {
            if (null == dto.getPurchaseNum() || null == dto.getUnitPrice()) {
                result.setMsg(dto.getFoodName() + "数量或价格不能为空");
                result.setCode(5001);
                return result;
            }
            dto.setMoney(dto.getPurchaseNum() * dto.getUnitPrice());
            batchOrdermxService.update(dto);
            result.setMsg(dto.getFoodName() + "修改成功");
            result.setCode(0);
            return result;
        }
    }

    @PostMapping("delete")
    @ApiOperation("删除")
    @LogOperation("删除")
    public Result delete(@RequestBody BatchOrdermxDTO dto){
        batchOrdermxService.delete1(dto.getBatchOrdermxId());
        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    @RequiresPermissions("canteens:batchordermx:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<BatchOrdermxDTO> list = batchOrdermxService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, list, BatchOrdermxExcel.class);
    }
    @PostMapping("getOrdermx")
    @ApiOperation("查询订单详情")
    public Result<Map> getOrdermx(@RequestBody BatchOrderDTO dto){
        Map map = batchOrderService.getOrdermx(dto);

        return new Result<Map>().ok(map);
    }
    @GetMapping("getTotalMoney")
    @ApiOperation("获取金额")
    public Result<BatchOrdermxDTO> getTotalMoney(Long batchOrderId){
        BatchOrdermxDTO dto = batchOrdermxService.getTotalMoney(batchOrderId);
        return new Result<BatchOrdermxDTO>().ok(dto);
    }
}